package com.ruoyi.web.controller.check;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.check.domain.*;
import com.ruoyi.check.service.IECheckListFormItemService;
import com.ruoyi.check.service.IECheckListFormNameService;
import com.ruoyi.check.service.IECheckListFormValueService;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.check.service.ISysProblemService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 检点项Controller
 * @author hugh.sun
 * @date 2023-02-22
 */

@Api(tags = "问题")
@RestController
@RequestMapping("/check/problem")
public class SysProblemController extends BaseController
{
    @Autowired
    private ISysProblemService sysProblemService;
    @Autowired
    private IECheckListFormValueService ieCheckListFormValueService;
    @Autowired
    private IECheckListFormItemService ieCheckListFormItemService;


    /**
     * 对应前端页面中的列/check/problem/operator
     */
    @GetMapping("/operator")
    public String operator(){

        LoginUser loginUser = SecurityUtils.getLoginUser();
        String username = loginUser.getUsername();
        return username;

    }


    /**
     *对应前端页面中的值
     * @param sysProblem
     * @return
     */
    @GetMapping("/testSearchzhi")
    public TableDataInfo testSearchzhi(SysProblem sysProblem)
    {
        //分页
        startPage();
        //假设有个全是值的集合
        List<SysProblem> list = sysProblemService.selectSysProblemList(sysProblem);

        return getDataTable(list);

    }



   /* @PreAuthorize("@ss.hasPermi('check:problem:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysProblem sysProblem)
    {
        startPage();
        //拿到转子表
        List<Eyulan> eyulans = new ArrayList<>();
        Eyulan eyulan = new Eyulan();

        String name = sysProblem.getName();
        List<SysProblem> sysProblems = new ArrayList<>();
        List<String> list = ieCheckListFormItemService.findLabel(name);
        list.forEach(i->{
            SysProblem sysProblem1 = new SysProblem();
            sysProblem1.setName(i);
            sysProblems.add(sysProblem1);
        });

        List<ECheckListFormValue> findanswer = ieCheckListFormValueService.findanswer(name);
       *//* findanswer.forEach(i->{
            Long itemId = i.getItemId();
            System.out.println(itemId);
            String url = i.geturl();
            System.out.println(url);
        });*//*
        eyulan.setSysProblems(sysProblems);
        eyulan.setFindanswer(findanswer);
        eyulans.add(eyulan);
        return getDataTable(eyulans);


    }
*/

    @PreAuthorize("@ss.hasPermi('check:problem:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysProblem sysProblem)
    {
        startPage();
        //拿到转子表

        String name = sysProblem.getName();
        List<SysProblem> sysProblems = new ArrayList<>();
        List<String> list = ieCheckListFormItemService.findLabel(name);
        list.forEach(i->{
            SysProblem sysProblem1 = new SysProblem();
            sysProblem1.setName(i);
            sysProblems.add(sysProblem1);
        });
        return getDataTable(sysProblems);


    }
    /**
     * 导出检点项列表
     */
    @PreAuthorize("@ss.hasPermi('check:problem:export')")
    @Log(title = "检点项", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysProblem sysProblem)
    {
        List<SysProblem> list = sysProblemService.selectSysProblemList(sysProblem);
        ExcelUtil<SysProblem> util = new ExcelUtil<SysProblem>(SysProblem.class);
        util.exportExcel(response, list, "检点项数据");
    }

    /**
     * 获取检点项详细信息
     */
    @PreAuthorize("@ss.hasPermi('check:problem:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysProblemService.selectSysProblemById(id));
    }

    /**
     * 新增检点项
     */

    @PreAuthorize("@ss.hasPermi('check:problem:add')")
    @Log(title = "检点项", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysProblem sysProblem)
    {
        return toAjax(sysProblemService.insertSysProblem(sysProblem));
    }

    /**
     * 修改检点项
     */

    @PreAuthorize("@ss.hasPermi('check:problem:edit')")
    @Log(title = "检点项", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysProblem sysProblem)
    {
        return toAjax(sysProblemService.updateSysProblem(sysProblem));
    }

    /**
     * 删除检点项
     */

    @PreAuthorize("@ss.hasPermi('check:problem:remove')")
    @Log(title = "检点项", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysProblemService.deleteSysProblemByIds(ids));
    }

    @Log(title = "拿到题目渲染列表")
    @GetMapping("/{name}")
    public List<SysProblem> findNameAndType(@PathVariable String name){

        List<SysProblem> nameAndType = sysProblemService.findNameAndType(name);

        return nameAndType;

    }
    @Log(title = "拿到答案渲染列表")
    @GetMapping("/allValue/")
    public List<ECheckListFormValue> allValue( String name){
        //返回一张表中所有的答案 并且按照id 升序排列
        //根据表名查到对应的问题   List<ECheckListFormValue>

        List<ECheckListFormValue> findanswer = ieCheckListFormValueService.findanswer(name);
        findanswer.forEach(i->{
            Long itemId = i.getItemId();
            System.out.println(itemId);
            String url = i.geturl();
            System.out.println(url);
        });
        return ieCheckListFormValueService.findanswer(name);
    }
    @ApiOperation("listan")
    @PreAuthorize("@ss.hasPermi('check:problem:list')")
    @GetMapping("/listAn")
    public EAllListVo listAn(SysProblem sysProblem)
    {
                    //拿到表头

                    String name = sysProblem.getName();

                    List<ECheckListFormItem> findtimu = ieCheckListFormItemService.findtimu(name);


                    List<WatchHead> watchHeads = new ArrayList<>();


                    EAllListVo eAllListVo = new EAllListVo();



                    findtimu.forEach(i->{
                        WatchHead watchHead = new WatchHead();
                        watchHead.setId(i.getId());

                        watchHead.setValue(i.getItemLabel());
                        watchHeads.add(watchHead);
                    });


                    //拿到表体
                    List<ECheckListFormValue> findanswer = ieCheckListFormValueService.findanswer(name);

                    HashMap<Long, List<ECheckListFormValue>> longListHashMap = new HashMap<>();

                     findanswer.forEach((i)->{

                         Long aLong = Long.valueOf(i.getSerialId());


                       try {
                           List<ECheckListFormValue> eCheckListFormValues1 = longListHashMap.get(aLong);
                           ECheckListFormValue eCheckListFormValue = new ECheckListFormValue();
                           eCheckListFormValue.setSerialId(i.getSerialId());
                           eCheckListFormValue.setItemId(i.getItemId());
                           eCheckListFormValue.seturl(i.geturl());
                           eCheckListFormValue.setCreateUser(i.getCreateUser());
                           eCheckListFormValue.setCreateTime(i.getCreateTime());
                           eCheckListFormValues1.add(eCheckListFormValue);
                           longListHashMap.put(aLong, eCheckListFormValues1);
                       }catch (Exception e){
                           List<ECheckListFormValue> eCheckListFormValues1 = new ArrayList<>();
                           ECheckListFormValue eCheckListFormValue = new ECheckListFormValue();
                           eCheckListFormValue.setSerialId(i.getSerialId());
                           eCheckListFormValue.setItemId(i.getItemId());
                           eCheckListFormValue.seturl(i.geturl());
                           eCheckListFormValue.setCreateUser(i.getCreateUser());
                           eCheckListFormValue.setCreateTime(i.getCreateTime());
                           eCheckListFormValues1.add(eCheckListFormValue);
                           longListHashMap.put(aLong, eCheckListFormValues1);

                       }


                    });
                    ArrayList<List> lists = new ArrayList<>();

                    longListHashMap.forEach((x,j)->{
                        lists.add(j);
                    });

                    eAllListVo.setWatchHeads(watchHeads);
                    eAllListVo.setLists(lists);


                    return eAllListVo;
                }


}
